/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.form.core;

import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Form bean for set filter
 * </p>
 * <p><a href="SetFilterForm.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2006/08/03 10:08:06 $
 * @struts.form name="setFilterForm"
 */
public class SetFilterForm extends BaseFilterForm {

	protected String beanId = null;
	protected String method = null;
	protected String label = null;
	protected String value = null;
	protected String firstCondition = FilterConditions.FIELD_IN_SET;
	protected Map selectedElements = Collections.synchronizedMap(new HashMap());

	/**
	 * Creates the new instance of SetFilterForm
	 */
	public SetFilterForm() {
	}

    /**
     * Returns ID of bean that will supply set values
     *
     * @return bean ID
     */
	public String getBeanId() {
		return beanId;
	}

    /**
     * Sets ID of bean that will supply set values
     *
     * @param beanId bean ID to set
     */
	public void setBeanId(String beanId) {
		this.beanId = beanId;
	}

    /**
     * Returns name of method that will be invoked on bean to obtain set
     *
     * @return name of method
     */
	public String getMethod() {
		return method;
	}

    /**
     * Sets name of method that will be invoked on bean to obtain set
     *
     * @param method name of method to set
     */
	public void setMethod(String method) {
		this.method = method;
	}

    /**
     * Returns name of field in class of bean which is showed in select
     *
     * @return label
     */
	public String getLabel() {
		return label;
	}

    /**
     * Sets name of field in class of bean which is showed in select
     *
     * @param label label to set
     */
	public void setLabel(String label) {
		this.label = label;
	}

    /**
     * Returns name of field in class of bean which corresponds to label
     *
     * @return value
     */
	public String getValue() {
		return value;
	}

    /**
     * Sets name of field in class of bean which corresponds to label
     *
     * @param value value to set
     */
	public void setValue(String value) {
		this.value = value;
	}

    /**
     * Returns first condition
     *
     * @return first condition
     */
	public String getFirstCondition() {
		return firstCondition;
	}

    /**
     * Sets first condition
     *
     * @param firstCondition first condition to set
     */
	public void setFirstCondition(String firstCondition) {
		this.firstCondition = firstCondition;
	}

    /**
     * Returns whether element with given name was selected
     *
     * @param key key of element
     * @return whether element with given name was selected
     */
	public Object getElement(String key) {
		if ( selectedElements.containsKey(key) ) {
			return Boolean.TRUE;
		} else {
			return Boolean.FALSE;
		}
	}

    /**
     * Sets selected element value
     *
     * @param key   key of selected element
     * @param value value of selected element
     */
	public void setElement(String key, Object value) {
		if ( log.isDebugEnabled() ) {
			log.debug("Setting element: key=" + key);
		}
		selectedElements.put(key, value);
	}

    /**
     * Returns mapping from keys of selected elements to their values
     *
     * @return mapping from keys of selected elements to their values
     */
	public Map getSelectedElements() {
		return selectedElements;
	}

    /**
     * Sets mapping from keys of selected elements to their values
     *
     * @param selectedElements mapping from keys of selected elements to their values
     */
	public void setSelectedElements(Map selectedElements) {
		this.selectedElements = selectedElements;
	}

	/**
	 * Resets all properties to their default values
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param request The non-http request we are proceeding
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);
		this.firstCondition = FilterConditions.FIELD_IN_SET;
		this.clearFilter = null;
		this.selectedElements = Collections.synchronizedMap(new HashMap());
		this.beanId = null;
		this.method = null;
		this.label = null;
		this.value = null;
	}

}